package main

import (
	"fmt"
	"log"
	"net"
)

func main() {
	udpConn, err := net.DialUDP("udp", nil, &net.UDPAddr{
		IP:   net.IPv4(192, 168, 3, 144),
		Port: 20000,
	})
	if err != nil {
		log.Fatalln("链接服务端失败", err)
		return
	}
	defer udpConn.Close()
	requestData := "hello world"
	_, err = udpConn.Write([]byte(requestData))
	if err != nil {
		log.Fatalln("发送数据失败", err)
		return
	}
	b := make([]byte, 1024)
	n, addr, err := udpConn.ReadFromUDP(b)
	if err != nil {
		log.Fatalln("接收服务端数据失败", err)
		return
	}
	responseData := string(b[0:n])
	fmt.Printf("发送方的IP地址是:%s,端口是:%d,数据的长度为:%d个字节,内容为:%s \n", addr.IP.String(), addr.Port, n, responseData)
}
